﻿var fileindexcount = 0;//上传图片，当前第几张图片
var file_upload_progress = '';
var hid_url = '';
var div_showImg = '';
var limit_fileCount = 0;
$(document).ready(function () {
});



// 选择图片
function uploadChange(obj, fileuploadprogress, hidurl, divshowImg, limitFileCount, filetype) {

    // debugger;
    file_upload_progress = fileuploadprogress;
    hid_url = hidurl;
    div_showImg = divshowImg;
    limit_fileCount = limitFileCount;
    var fileCount = obj.files.length;
    for (var ifile = 0; ifile < fileCount; ifile++) {
        var filetemp = obj.files[ifile];
        var strext = filetemp.name.substring(filetemp.name.lastIndexOf('.')).toLowerCase();
        if (filetype == 0) {
            //图片
            if (strext != ".jpg" && strext != ".gif" && strext != ".jpeg" && strext != ".bmp" && strext != ".png") {
                alert("请上传图片格式的文件，谢谢。" + filetemp.name.toString());
                return false;
            }
        }
        else if (filetype == 1) {
            //文件
            //if (strext != ".jpg" && strext != ".gif" && strext != ".jpeg" && strext != ".bmp" && strext != ".png") {
            //    alert("请上传图片格式的文件，谢谢。" + filetemp.name.toString());
            //    return false;
            //}
        }
    }
    fileindexcount = 0;
    if (checkFileCount()) {
        //没超出限制，可以上传
        upfile(obj.files, fileCount);
    }
}

function upfile(filetxtobj, fileCount) {
    var fileobj = filetxtobj[fileindexcount];
    ////////压缩图片
    // var file = fileobj;//this.files[ifile];
    var file_upload = fileobj;// file;



    var strext = file_upload.name.substring(file_upload.name.lastIndexOf('.')).toLowerCase();
    var picName = JSGuid() + strext;

    // debugger
    $("#" + file_upload_progress).show();
    document.getElementById(file_upload_progress).src = "/common/images/uploading.gif";

    var reader = new FileReader();
    reader.readAsDataURL(file_upload);
    //原文件名
    reader.onload = function (e) { // reader onload start
        // ajax 上传图片

        $.post("/Common/addFileToServerforBase64", { "flag": Math.random(), "oldfilename": picName, base64stringdata: e.target.result }, function (data) {


            var Result = eval(data);

            if (Result.respCode != "200") {
                document.getElementById(file_upload_progress).src = "/common/images/err.jpg";
                return;
            }
            else {

                fileindexcount = fileindexcount + 1;
                var file_uploadurl = Result.data;

                addFile(file_uploadurl);
                $("#" + file_upload_progress).show();
                document.getElementById(file_upload_progress).src = "/common/images/ok.png";

                //  alert(fileindexcount);
                if (fileindexcount < fileCount) {
                    //重新调用
                    upfile(filetxtobj, fileCount);
                }

            }

        }, 'json');
    } // reader onload end
}
function checkFileCount() {
    var hidfile_uploadurl = document.getElementById(hid_url).value;
    if (hidfile_uploadurl == "") {
        return true;
    }
    var cou = hidfile_uploadurl.split(",").length;
    if (cou >= limit_fileCount) {
        document.getElementById(file_upload_progress).src = "/common/images/err.jpg";
        alert("添加失败，文件数量超出" + limit_fileCount + "张限制");
        return false;
    }
    else {
        return true;
    }
}

function delFile(url, fileurl, filedivshowimg) {
    //删除图片
    var imgObj = document.getElementById(url);
    var hidimgurl = document.getElementById(fileurl).value;
    hidimgurl = hidimgurl.replace(imgObj.name, "");
    document.getElementById(fileurl).value = hidimgurl;

    ShowFile(fileurl, filedivshowimg);
}
function addFile(url) {
    //添加图片
    var hidimgurl = document.getElementById(hid_url).value;

    if (hidimgurl != "") {
        hidimgurl = hidimgurl + "," + url;
    }
    else {
        hidimgurl = url;
    }

    document.getElementById(hid_url).value = hidimgurl;

    ShowFile(hid_url, div_showImg);
}
function ShowFile(fileurl, filedivshowImg) {
    //显示图片
    var hidimgurl = document.getElementById(fileurl).value;
    document.getElementById(filedivshowImg).innerHTML = "";
    for (var i = 0; i < hidimgurl.split(",").length; i++) {
        if (hidimgurl.split(",")[i] != "") {
            var strext = hidimgurl.split(",")[i].substring(hidimgurl.split(",")[i].lastIndexOf('.')).toLowerCase();
            if (strext == ".jpg" || strext == ".gif" || strext == ".jpeg" || strext == ".bmp" || strext == ".png") {
                document.getElementById(filedivshowImg).innerHTML += "<div style='margin:1px;float:left;text-align:center'><div style='margin:1px;border: solid 1px #e4e1e1; padding: 2px;'><a href='" + hidimgurl.split(",")[i].replace("/small/", "/big/") + "' target='_blank'><img src='" + hidimgurl.split(",")[i] + "' name='" + hidimgurl.split(",")[i] + "' id='" + fileurl + "uimg_" + (i + 1) + "'  style='height:70px;width:80px;margin:0px; padding:0px;'/></a></div><input type='button' style='width:40px;line-height:30px;height:30px;color:black' onclick=\"delFile('" + fileurl + "uimg_" + (i + 1) + "','" + fileurl + "','" + filedivshowImg + "')\" value='删除' /></div>";
                //图片
            }
            else {
                document.getElementById(filedivshowImg).innerHTML += "<div style='margin:1px;float:left;text-align:center'><div style='margin:1px;border: solid 1px #e4e1e1; padding: 2px;'><a href='" + hidimgurl.split(",")[i].replace("/small/", "/big/") + "' target='_blank'>文件</a></div><input type='button' style='width:40px;line-height:30px;height:30px;color:black' onclick=\"delFile('" + fileurl + "uimg_" + (i + 1) + "','" + fileurl + "','" + filedivshowImg + "')\" value='删除' /></div>";
                //文件
            }
        }
    }
}


function JSGuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}



